<?php

/*
 * This class is used for common same function.
 */

class Model_Quotes extends Model_Base {
	
	protected $_name,$_primaryKey;
    
    public function init() {
        $this->_name = $this->QUOTES_TBL;
        $this->_primaryKey = 'quote_id';
    }
	
	 
	public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
	
	public function getQuotescurrencyArray() {
        return array(
            "" => "-- Select Quotes Currency--",
            "us_dollar" => "US Dollar",
            "euro" => "EURO"
        );
    }
	
	public function getQuotesMainStatusArray() {
        return array(
            "" => "",
            "draft" => "Draft",
            "sent_to_client" => "Sent to client",
			"being_reviewed _by_client" => "Being reviewed by client",
            "approved" => "Approved",
			"no_response" => "No response",
			"declined" => "Declined"
        );
    }
	
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('quotes' => $this->QUOTES_TBL))
                ->where('quote_id = ?', $id)
				->where("quote_status != 2");
     
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	 public function fetchQuoteData($where = "", $limit = "", $offset = "0", $order = "quote_id", $sort = "ASC") {
       $select = $this->select()
                ->from(array("quotes" => $this->QUOTES_TBL))
				->setIntegrityCheck(false)
				->join(array('lgn' => $this->LOGIN_TBL), "quotes.quote_responsible = lgn.lgn_id")
				->where("quote_status != 2");
        if ($where != "") {
            $select->where($where);
        }
		//echo $select;exit;
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	public function  fetchQuote($where = ""){
        
        $select = $this->select()->setIntegrityCheck(false)
                ->from(array("quotes" => $this->QUOTES_TBL))
                ->where("quote_status != 2");
		if ($where != '') {
            $select->where($where);
        }
		
        $result = $this->fetchAll($select);
		//echo $select;
		
        $finalArray[''] = '';
        foreach ($result as $res) {
            
            $finalArray[$res['quote_id']] = $res['quote_subject'];
        }
		
        return $finalArray;
		
    }
	
	public function getQuoteCount($where = "") {
        $select = $this->select()
                ->from(array("quotes" => $this->QUOTES_TBL), Array('cnt' => 'count(quote_id)'))
			    ->where("quote_status != 2");
        if ($where != '') {
            $select->where($where);
        }

        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
	
	public function deleteQuote($quoteArray) {
        if (is_array($quoteArray)) {
            foreach ($quoteArray as $_quote) {
                $quoteData = Array();
                $quoteData = $this->fetchEntryById($_quote);
                if (count($quoteData) > 0) {
                    $data = array("quote_status" => "2");
                    $this->update($data, "quote_id =" . (int) $_quote);
                }
            }
        } else {
            $quote_id = $quoteArray;
            $quoteData = $this->fetchEntryById($quote_id);
            if (count($quoteData) > 0) {
                $data = array("quote_status" => "2");
                $this->update($data, "quote_id =" . $quote_id);
            }
        }
    }
}